package com.march.utils;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

public class JWTUtils2 {

    private static final long time = 1000*60;
    private static final String signature = "admin";

    //将ID存入setSubject中
    public static String createToken(String payload){
        JwtBuilder jwtBuilder = Jwts.builder();
        jwtBuilder
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //payload
                .setSubject(payload)
                .setId(UUID.randomUUID().toString())
                .setExpiration(new Date(System.currentTimeMillis() + time))
                .signWith(SignatureAlgorithm.HS256, signature);

//        jwtBuilder.setClaims(payload);

        return jwtBuilder.compact();
    }

    public static String createToken(Map payload){
        JwtBuilder jwtBuilder = Jwts.builder();
        jwtBuilder
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //payload
                .setSubject("admin-text")
                .setId(UUID.randomUUID().toString())
                .setExpiration(new Date(System.currentTimeMillis() + time))
                .signWith(SignatureAlgorithm.HS256, signature);

        jwtBuilder.setClaims(payload);

        return jwtBuilder.compact();
    }
    public static Boolean checkToken(String token){

        if(token == null){
            return false;
        }
        try {
            Jwts.parser().setSigningKey(signature).parseClaimsJws(token);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
